$(document).ready(function() {
	var ChannelId=getItems("Channel") != null ? getItems("Channel") : "mychannel";
	var FabricFlag=getItems("networkName");
	var Token=getItems("token");
	
	if(loginToken){
		$(".Reminder").hide();
		var QueryAllPeerUrl = '/api/block/public/queryAllPeer';
		var QueryAllPeerData = {
			"RequestType": "QueryAllPeerReq",
			"ChannelId":ChannelId,
			"FabricFlag":FabricFlag
		};
		$(".cover").show();
		requestPost(QueryAllPeerUrl, QueryAllPeerData,0);
	}else{
		$(".Reminder").show();
		var QueryAllPeerUrl = '/api/block/cache/queryAllPeer';
		var QueryAllPeerData = {
			"RequestType": "QueryAllPeerReq",
			"ChannelId":ChannelId,
			"FabricFlag":FabricFlag
		};
		$(".cover").show();
		requestPost(QueryAllPeerUrl, QueryAllPeerData,0);
	}

	function requestPost(url, Data) {
		var oData = Data;
		$.ajax({
			type: "POST",
			contentType: "application/json",
			url: url,
			data: JSON.stringify(oData),
			dataType: "json",
			headers:{
				Authorization:Token,
			},
			success: function(data) {
				$(".cover").hide();
			
				if(data.code==200){
					if(data.status==40101){
						$(".containerBox").html('<div class="NoData">登录已失效，请重新登录！</div>');
						$(".NoData").show();
						$(".NoData").css({
							marginTop:30
						})
					}else{
						var oData=JSON.parse(data.data);
						var peerData=oData.PeerInfoList.sort(compare("MspId"));
						var str='';
						for(var i=0;i<peerData.length;i++){
							str+='<div class="nodeCtBox"><div class="nodeCtTop"><div class="row"><div class="col-lg-5 col-md-5 col-sm-12 col-xs-12"><div class="nameBox"><span>Peer节点地址</span><a>'+peerData[i].Url+'</a></div></div><div class="col-lg-7 col-md-7 col-sm-12 col-xs-12"><div class="organization"><span>组织</span><a>'+peerData[i].MspId+'</a></div></div></div></div></div>';
						}
						$(".nodeCt").html(str);   
					}
				}else{
					$(".containerBox").html('<div class="NoData">暂无数据</div>');
					$(".NoData").show();
					$(".NoData").css({
						marginTop:30
					})
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				$(".cover").hide();
				$(".containerBox").html('<div class="NoData">暂无数据</div>');
				$(".NoData").show();
				$(".NoData").css({
					marginTop:30
				})
				console.log("调取sns接口出错");
				console.log(XMLHttpRequest.status);
				console.log(XMLHttpRequest.readyState);
				console.log(textStatus);
			}
		});
	}
 	var compare = function (prop) {
	    return function (obj1, obj2) {
	        var val1 = obj1[prop];
	        var val2 = obj2[prop];
	        if (!isNaN(Number(val1)) && !isNaN(Number(val2))) {
	            val1 = Number(val1);
	            val2 = Number(val2);
	        }
	        if (val1 < val2) {
	            return -1;
	        } else if (val1 > val2) {
	            return 1;
	        } else {
	            return 0;
	        }            
	    } 
	}
})
